package com.fulicarpet.shop.ui.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fulicarpet.shop.R;
import com.fulicarpet.shop.ui.activity.MainActivity;
import com.fulicarpet.shop.utils.VoicePlay;

import cn.jpush.android.api.JPushInterface;

/**
 * 自定义接收器
 * <p/>
 * 如果不定义这个 Receiver，则：
 * 1) 默认用户会打开主界面
 * 2) 接收不到自定义消息
 */
public class MyReceiver extends BroadcastReceiver {
    private static final String TAG = "JIGUANG-Example";

    @Override
    public void onReceive(Context context, Intent intent) {
        try {
            Bundle bundle = intent.getExtras();
            String action = intent.getAction();
            switch (action){
                case JPushInterface.ACTION_REGISTRATION_ID:
                    String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
                    Log.d(TAG, "[MyReceiver] 接收Registration Id : " + regId);
                    break;
                case JPushInterface.ACTION_MESSAGE_RECEIVED:
                    Log.d(TAG, "[MyReceiver] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE));
                    break;
                case JPushInterface.ACTION_NOTIFICATION_RECEIVED:
                    System.out.println("收到了通知");
                    String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
                    JSONObject object = JSON.parseObject(extras);
                    String msgType = object.getString("msgType");
                    if ("2".equals(msgType)){
                        VoicePlay.with(context).play(R.raw.order_tips);
                    }
                    break;
                case JPushInterface.ACTION_NOTIFICATION_OPENED:
                    openNotification(context,bundle);
                    break;
                case JPushInterface.ACTION_RICHPUSH_CALLBACK:
                    Log.d(TAG, "[MyReceiver] 用户收到到RICH PUSH CALLBACK: " + bundle.getString(JPushInterface.EXTRA_EXTRA));
                    //在这里根据 JPushInterface.EXTRA_EXTRA 的内容处理代码，比如打开新的Activity， 打开一个网页等..
                    break;
                case JPushInterface.ACTION_CONNECTION_CHANGE:
                    boolean connected = intent.getBooleanExtra(JPushInterface.EXTRA_CONNECTION_CHANGE, false);
                    Log.w(TAG, "[MyReceiver]" + action +" connected state change to "+connected);
                    break;
                default:
                    Log.d(TAG, "[MyReceiver] Unhandled intent - " + action);
            }
        } catch (Exception e){

        }
    }

    private void openNotification(Context context, Bundle bundle){
        Intent intent = new Intent(context, MainActivity.class);  //自定义打开的界面
        String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
        JSONObject object = JSON.parseObject(extras);
        String msgType = object.getString("msgType");
        String url = object.getString("url");
        intent.putExtra("url",url);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }
}

